
-----------------------------------------------------------------------
-- lecutre_hour_defs
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_lecutre_hour_defs_lectures')
    ALTER TABLE lecutre_hour_defs DROP CONSTRAINT FK_lecutre_hour_defs_lectures;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'lecutre_hour_defs')
BEGIN
    DECLARE @reftable_1 nvarchar(60), @constraintname_1 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'lecutre_hour_defs'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_1, @constraintname_1
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_1+' drop constraint '+@constraintname_1)
        FETCH NEXT from refcursor into @reftable_1, @constraintname_1
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE lecutre_hour_defs
END

CREATE TABLE lecutre_hour_defs
(
    id INT NOT NULL IDENTITY,
    lecture_id INT NOT NULL,
    week_day TINYINT(1,0) NOT NULL,
    start_hour VARCHAR(32) NOT NULL,
    end_hour VARCHAR(32) NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT lecutre_hour_defs_PK PRIMARY KEY (id)
);

CREATE INDEX PK_lecutre_hour_defs ON lecutre_hour_defs (id);

BEGIN
ALTER TABLE lecutre_hour_defs ADD CONSTRAINT FK_lecutre_hour_defs_lectures FOREIGN KEY (lecture_id) REFERENCES lectures (id)
END
;

-----------------------------------------------------------------------
-- event_order_proposal_free_days
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_request_proposal_free_day_event_requests')
    ALTER TABLE event_order_proposal_free_days DROP CONSTRAINT FK_event_request_proposal_free_day_event_requests;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_order_proposal_free_days')
BEGIN
    DECLARE @reftable_2 nvarchar(60), @constraintname_2 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_order_proposal_free_days'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_2, @constraintname_2
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_2+' drop constraint '+@constraintname_2)
        FETCH NEXT from refcursor into @reftable_2, @constraintname_2
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_order_proposal_free_days
END

CREATE TABLE event_order_proposal_free_days
(
    id INT NOT NULL IDENTITY,
    start_date DATETIME(16,3) NOT NULL,
    end_date DATETIME(16,3) NOT NULL,
    event_order_id INT NOT NULL,
    selected INT DEFAULT 0 NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_order_proposal_free_days_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_order_proposal_free_days ON event_order_proposal_free_days (id);

BEGIN
ALTER TABLE event_order_proposal_free_days ADD CONSTRAINT FK_event_request_proposal_free_day_event_requests FOREIGN KEY (event_order_id) REFERENCES event_orders (id)
END
;

-----------------------------------------------------------------------
-- event_request_comments
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_request_comments_event_requests')
    ALTER TABLE event_request_comments DROP CONSTRAINT FK_event_request_comments_event_requests;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_request_comments')
BEGIN
    DECLARE @reftable_3 nvarchar(60), @constraintname_3 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_request_comments'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_3, @constraintname_3
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_3+' drop constraint '+@constraintname_3)
        FETCH NEXT from refcursor into @reftable_3, @constraintname_3
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_request_comments
END

CREATE TABLE event_request_comments
(
    id INT NOT NULL IDENTITY,
    event_request_id INT NOT NULL,
    text VARCHAR(MAX)(2147483647) NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_request_comments_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_request_comments ON event_request_comments (id);

BEGIN
ALTER TABLE event_request_comments ADD CONSTRAINT FK_event_request_comments_event_requests FOREIGN KEY (event_request_id) REFERENCES event_requests (id)
END
;

-----------------------------------------------------------------------
-- event_order_comments
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_order_comments_event_orders')
    ALTER TABLE event_order_comments DROP CONSTRAINT FK_event_order_comments_event_orders;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_order_comments')
BEGIN
    DECLARE @reftable_4 nvarchar(60), @constraintname_4 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_order_comments'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_4, @constraintname_4
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_4+' drop constraint '+@constraintname_4)
        FETCH NEXT from refcursor into @reftable_4, @constraintname_4
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_order_comments
END

CREATE TABLE event_order_comments
(
    id INT NOT NULL IDENTITY,
    event_request_id INT NOT NULL,
    text VARCHAR(MAX)(2147483647) NOT NULL,
    is_supperior INT NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_order_comments_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_order_comments ON event_order_comments (id);

BEGIN
ALTER TABLE event_order_comments ADD CONSTRAINT FK_event_order_comments_event_orders FOREIGN KEY (event_request_id) REFERENCES event_orders (id)
END
;

-----------------------------------------------------------------------
-- lecture_presences
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_lecture_presences_lecutre_hours')
    ALTER TABLE lecture_presences DROP CONSTRAINT FK_lecture_presences_lecutre_hours;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_lecture_presences_users')
    ALTER TABLE lecture_presences DROP CONSTRAINT FK_lecture_presences_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'lecture_presences')
BEGIN
    DECLARE @reftable_5 nvarchar(60), @constraintname_5 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'lecture_presences'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_5, @constraintname_5
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_5+' drop constraint '+@constraintname_5)
        FETCH NEXT from refcursor into @reftable_5, @constraintname_5
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE lecture_presences
END

CREATE TABLE lecture_presences
(
    id INT NOT NULL IDENTITY,
    lecture_hour_id INT NOT NULL,
    user_id INT NOT NULL,
    start_date DATETIME(16,3) NOT NULL,
    end_date DATETIME(16,3) NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT lecture_presences_PK PRIMARY KEY (id)
);

CREATE INDEX PK_lecture_presences ON lecture_presences (id);

BEGIN
ALTER TABLE lecture_presences ADD CONSTRAINT FK_lecture_presences_lecutre_hours FOREIGN KEY (lecture_hour_id) REFERENCES lecutre_hours (id)
END
;

BEGIN
ALTER TABLE lecture_presences ADD CONSTRAINT FK_lecture_presences_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- event_request_has_calendar_event
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_request_has_calendar_event_calendar_events')
    ALTER TABLE event_request_has_calendar_event DROP CONSTRAINT FK_event_request_has_calendar_event_calendar_events;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_request_has_calendar_event_event_requests')
    ALTER TABLE event_request_has_calendar_event DROP CONSTRAINT FK_event_request_has_calendar_event_event_requests;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_request_has_calendar_event')
BEGIN
    DECLARE @reftable_6 nvarchar(60), @constraintname_6 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_request_has_calendar_event'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_6, @constraintname_6
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_6+' drop constraint '+@constraintname_6)
        FETCH NEXT from refcursor into @reftable_6, @constraintname_6
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_request_has_calendar_event
END

CREATE TABLE event_request_has_calendar_event
(
    calendar_event_id INT NOT NULL,
    event_request_id INT NOT NULL,
    start_date DATETIME(16,3) NULL,
    end_date DATETIME(16,3) NULL,
    CONSTRAINT event_request_has_calendar_event_PK PRIMARY KEY (calendar_event_id,event_request_id)
);

CREATE INDEX PK_event_request_has_calendar_event_1 ON event_request_has_calendar_event (calendar_event_id,event_request_id);

BEGIN
ALTER TABLE event_request_has_calendar_event ADD CONSTRAINT FK_event_request_has_calendar_event_calendar_events FOREIGN KEY (calendar_event_id) REFERENCES calendar_events (id)
END
;

BEGIN
ALTER TABLE event_request_has_calendar_event ADD CONSTRAINT FK_event_request_has_calendar_event_event_requests FOREIGN KEY (event_request_id) REFERENCES event_requests (id)
END
;

-----------------------------------------------------------------------
-- lecutre_hours
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_lecutre_hours_lectures')
    ALTER TABLE lecutre_hours DROP CONSTRAINT FK_lecutre_hours_lectures;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'lecutre_hours')
BEGIN
    DECLARE @reftable_7 nvarchar(60), @constraintname_7 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'lecutre_hours'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_7, @constraintname_7
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_7+' drop constraint '+@constraintname_7)
        FETCH NEXT from refcursor into @reftable_7, @constraintname_7
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE lecutre_hours
END

CREATE TABLE lecutre_hours
(
    id INT NOT NULL IDENTITY,
    lecture_id INT NOT NULL,
    start_date DATETIME(16,3) NOT NULL,
    end_date DATETIME(16,3) NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT lecutre_hours_PK PRIMARY KEY (id)
);

CREATE INDEX PK_lecutre_hours ON lecutre_hours (id);

BEGIN
ALTER TABLE lecutre_hours ADD CONSTRAINT FK_lecutre_hours_lectures FOREIGN KEY (lecture_id) REFERENCES lectures (id)
END
;

-----------------------------------------------------------------------
-- auth_history
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_auth_history_users')
    ALTER TABLE auth_history DROP CONSTRAINT FK_auth_history_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'auth_history')
BEGIN
    DECLARE @reftable_8 nvarchar(60), @constraintname_8 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'auth_history'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_8, @constraintname_8
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_8+' drop constraint '+@constraintname_8)
        FETCH NEXT from refcursor into @reftable_8, @constraintname_8
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE auth_history
END

CREATE TABLE auth_history
(
    id INT NOT NULL IDENTITY,
    user_id INT NULL,
    ad_login VARCHAR(80) NULL,
    login VARCHAR(100) NULL,
    ext_ip VARCHAR(15) NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT auth_history_PK PRIMARY KEY (id)
);

CREATE INDEX PK_auth_history ON auth_history (id);

BEGIN
ALTER TABLE auth_history ADD CONSTRAINT FK_auth_history_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- event_request_types
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_request_types')
BEGIN
    DECLARE @reftable_9 nvarchar(60), @constraintname_9 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_request_types'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_9, @constraintname_9
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_9+' drop constraint '+@constraintname_9)
        FETCH NEXT from refcursor into @reftable_9, @constraintname_9
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_request_types
END

CREATE TABLE event_request_types
(
    id INT NOT NULL IDENTITY,
    name_en CHAR(20) NULL,
    name_pl VARCHAR(250) NOT NULL,
    code VARCHAR(100) NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    last_modified DATETIME(16,3) NULL,
    CONSTRAINT event_request_types_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_request_types ON event_request_types (id);

-----------------------------------------------------------------------
-- event_requests
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_requests_event_request_types')
    ALTER TABLE event_requests DROP CONSTRAINT FK_event_requests_event_request_types;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_requests')
BEGIN
    DECLARE @reftable_10 nvarchar(60), @constraintname_10 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_requests'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_10, @constraintname_10
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_10+' drop constraint '+@constraintname_10)
        FETCH NEXT from refcursor into @reftable_10, @constraintname_10
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_requests
END

CREATE TABLE event_requests
(
    id INT NOT NULL IDENTITY,
    event_request_type_id INT NOT NULL,
    status TINYINT(1,0) NOT NULL,
    corporate_output INT NULL,
    work_off_date VARCHAR(20) NULL,
    comment VARCHAR(MAX)(2147483647) NULL,
    recompense_type TINYINT(1,0) NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_requests_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_requests ON event_requests (id);

BEGIN
ALTER TABLE event_requests ADD CONSTRAINT FK_event_requests_event_request_types FOREIGN KEY (event_request_type_id) REFERENCES event_request_types (id)
END
;

-----------------------------------------------------------------------
-- event_request_proposal_free_day
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_request_proposal_free_day_event_requests')
    ALTER TABLE event_request_proposal_free_day DROP CONSTRAINT FK_event_request_proposal_free_day_event_requests;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_request_proposal_free_day')
BEGIN
    DECLARE @reftable_11 nvarchar(60), @constraintname_11 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_request_proposal_free_day'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_11, @constraintname_11
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_11+' drop constraint '+@constraintname_11)
        FETCH NEXT from refcursor into @reftable_11, @constraintname_11
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_request_proposal_free_day
END

CREATE TABLE event_request_proposal_free_day
(
    id INT NOT NULL IDENTITY,
    start_date DATETIME(16,3) NOT NULL,
    end_date DATETIME(16,3) NOT NULL,
    event_request_id INT NOT NULL,
    selected INT DEFAULT 0 NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_request_proposal_free_day_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_request_proposal_free_day ON event_request_proposal_free_day (id);

BEGIN
ALTER TABLE event_request_proposal_free_day ADD CONSTRAINT FK_event_request_proposal_free_day_event_requests FOREIGN KEY (event_request_id) REFERENCES event_requests (id)
END
;

-----------------------------------------------------------------------
-- calendar_days
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_calendar_days_calendars')
    ALTER TABLE calendar_days DROP CONSTRAINT FK_calendar_days_calendars;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'calendar_days')
BEGIN
    DECLARE @reftable_12 nvarchar(60), @constraintname_12 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'calendar_days'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_12, @constraintname_12
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_12+' drop constraint '+@constraintname_12)
        FETCH NEXT from refcursor into @reftable_12, @constraintname_12
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE calendar_days
END

CREATE TABLE calendar_days
(
    id INT NOT NULL IDENTITY,
    calendar_id INT NOT NULL,
    day VARCHAR(20) NOT NULL,
    type INT NOT NULL,
    work_hours INT NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT calendar_days_PK PRIMARY KEY (id)
);

CREATE INDEX PK_calendar_days ON calendar_days (id);

BEGIN
ALTER TABLE calendar_days ADD CONSTRAINT FK_calendar_days_calendars FOREIGN KEY (calendar_id) REFERENCES calendars (id)
END
;

-----------------------------------------------------------------------
-- calendars
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'calendars')
BEGIN
    DECLARE @reftable_13 nvarchar(60), @constraintname_13 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'calendars'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_13, @constraintname_13
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_13+' drop constraint '+@constraintname_13)
        FETCH NEXT from refcursor into @reftable_13, @constraintname_13
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE calendars
END

CREATE TABLE calendars
(
    id INT NOT NULL IDENTITY,
    name VARCHAR(100) NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT calendars_PK PRIMARY KEY (id)
);

CREATE INDEX PK_calendars ON calendars (id);

-----------------------------------------------------------------------
-- users
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_users_positions')
    ALTER TABLE users DROP CONSTRAINT FK_users_positions;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_users_units')
    ALTER TABLE users DROP CONSTRAINT FK_users_units;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'users')
BEGIN
    DECLARE @reftable_14 nvarchar(60), @constraintname_14 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'users'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_14, @constraintname_14
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_14+' drop constraint '+@constraintname_14)
        FETCH NEXT from refcursor into @reftable_14, @constraintname_14
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE users
END

CREATE TABLE users
(
    id INT NOT NULL IDENTITY,
    ext_id INT NOT NULL,
    position_id INT NOT NULL,
    unit_id INT NOT NULL,
    fname VARCHAR(30) NULL,
    lname VARCHAR(50) NULL,
    email VARCHAR(80) NULL,
    optional_email VARCHAR(80) NULL,
    password VARCHAR(64) NULL,
    ad_login VARCHAR(30) NULL,
    is_superior INT DEFAULT 0 NOT NULL,
    deleted DATETIME(16,3) NULL,
    active INT DEFAULT 1 NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT users_PK PRIMARY KEY (id)
);

CREATE INDEX PK_users ON users (id);

BEGIN
ALTER TABLE users ADD CONSTRAINT FK_users_positions FOREIGN KEY (position_id) REFERENCES positions (id)
END
;

BEGIN
ALTER TABLE users ADD CONSTRAINT FK_users_units FOREIGN KEY (unit_id) REFERENCES units (id)
END
;

-----------------------------------------------------------------------
-- calendar_events
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_calendar_events_calendar_days')
    ALTER TABLE calendar_events DROP CONSTRAINT FK_calendar_events_calendar_days;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_calendar_events_event_defs')
    ALTER TABLE calendar_events DROP CONSTRAINT FK_calendar_events_event_defs;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_calendar_events_users')
    ALTER TABLE calendar_events DROP CONSTRAINT FK_calendar_events_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'calendar_events')
BEGIN
    DECLARE @reftable_15 nvarchar(60), @constraintname_15 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'calendar_events'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_15, @constraintname_15
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_15+' drop constraint '+@constraintname_15)
        FETCH NEXT from refcursor into @reftable_15, @constraintname_15
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE calendar_events
END

CREATE TABLE calendar_events
(
    id INT NOT NULL IDENTITY,
    user_id INT NOT NULL,
    calendar_day_id INT NOT NULL,
    event_def_id INT NOT NULL,
    start_date DATETIME(16,3) NOT NULL,
    end_date DATETIME(16,3) NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT calendar_events_PK PRIMARY KEY (id)
);

CREATE INDEX PK_calendar_events ON calendar_events (id);

BEGIN
ALTER TABLE calendar_events ADD CONSTRAINT FK_calendar_events_calendar_days FOREIGN KEY (calendar_day_id) REFERENCES calendar_days (id)
END
;

BEGIN
ALTER TABLE calendar_events ADD CONSTRAINT FK_calendar_events_event_defs FOREIGN KEY (event_def_id) REFERENCES event_defs (id)
END
;

BEGIN
ALTER TABLE calendar_events ADD CONSTRAINT FK_calendar_events_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- event_orders
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_event_orders_users')
    ALTER TABLE event_orders DROP CONSTRAINT FK_event_orders_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_orders')
BEGIN
    DECLARE @reftable_16 nvarchar(60), @constraintname_16 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_orders'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_16, @constraintname_16
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_16+' drop constraint '+@constraintname_16)
        FETCH NEXT from refcursor into @reftable_16, @constraintname_16
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_orders
END

CREATE TABLE event_orders
(
    id INT NOT NULL IDENTITY,
    user_id INT NULL,
    status TINYINT(1,0) NULL,
    hours TINYINT(1,0) NOT NULL,
    start_time VARCHAR(32) NULL,
    end_time VARCHAR(32) NULL,
    reason VARCHAR(MAX)(2147483647) NULL,
    recompense_type TINYINT(1,0) NOT NULL,
    home_office INT NULL,
    comment VARCHAR(MAX)(2147483647) NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_orders_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_orders ON event_orders (id);

BEGIN
ALTER TABLE event_orders ADD CONSTRAINT FK_event_orders_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- positions
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'positions')
BEGIN
    DECLARE @reftable_17 nvarchar(60), @constraintname_17 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'positions'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_17, @constraintname_17
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_17+' drop constraint '+@constraintname_17)
        FETCH NEXT from refcursor into @reftable_17, @constraintname_17
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE positions
END

CREATE TABLE positions
(
    id INT NOT NULL IDENTITY,
    ext_id VARCHAR(100) NOT NULL,
    name_pl VARCHAR(100) NULL,
    name_en VARCHAR(100) NULL,
    grade TINYINT(1,0) NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT positions_PK PRIMARY KEY (id)
);

CREATE INDEX PK_positions ON positions (id);

-----------------------------------------------------------------------
-- lectures
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_lectures_users')
    ALTER TABLE lectures DROP CONSTRAINT FK_lectures_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'lectures')
BEGIN
    DECLARE @reftable_18 nvarchar(60), @constraintname_18 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'lectures'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_18, @constraintname_18
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_18+' drop constraint '+@constraintname_18)
        FETCH NEXT from refcursor into @reftable_18, @constraintname_18
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE lectures
END

CREATE TABLE lectures
(
    id INT NOT NULL IDENTITY,
    group_name_en VARCHAR(100) NOT NULL,
    group_name_pl VARCHAR(100) NOT NULL,
    language_name_pl VARCHAR(100) NULL,
    language_name_en VARCHAR(100) NULL,
    teacher_id INT NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT lectures_PK PRIMARY KEY (id)
);

CREATE INDEX PK_lectures ON lectures (id);

BEGIN
ALTER TABLE lectures ADD CONSTRAINT FK_lectures_users FOREIGN KEY (teacher_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- units
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_units_units')
    ALTER TABLE units DROP CONSTRAINT FK_units_units;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'units')
BEGIN
    DECLARE @reftable_19 nvarchar(60), @constraintname_19 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'units'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_19, @constraintname_19
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_19+' drop constraint '+@constraintname_19)
        FETCH NEXT from refcursor into @reftable_19, @constraintname_19
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE units
END

CREATE TABLE units
(
    id INT NOT NULL IDENTITY,
    parent_id INT NULL,
    ext_id VARCHAR(100) NOT NULL,
    name_pl VARCHAR(100) NULL,
    name_en VARCHAR(100) NULL,
    typ CHAR(1) NULL,
    marka CHAR(3) NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT units_PK PRIMARY KEY (id)
);

CREATE INDEX PK_units ON units (id);

BEGIN
ALTER TABLE units ADD CONSTRAINT FK_units_units FOREIGN KEY (parent_id) REFERENCES units (id)
END
;

-----------------------------------------------------------------------
-- user_has_lecture
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_has_lecture_lectures')
    ALTER TABLE user_has_lecture DROP CONSTRAINT FK_user_has_lecture_lectures;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_has_lecture_users')
    ALTER TABLE user_has_lecture DROP CONSTRAINT FK_user_has_lecture_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'user_has_lecture')
BEGIN
    DECLARE @reftable_20 nvarchar(60), @constraintname_20 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'user_has_lecture'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_20, @constraintname_20
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_20+' drop constraint '+@constraintname_20)
        FETCH NEXT from refcursor into @reftable_20, @constraintname_20
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE user_has_lecture
END

CREATE TABLE user_has_lecture
(
    user_id INT NOT NULL,
    lecture_id INT NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT user_has_lecture_PK PRIMARY KEY (user_id,lecture_id)
);

CREATE INDEX PK_user_has_lecture ON user_has_lecture (user_id,lecture_id);

BEGIN
ALTER TABLE user_has_lecture ADD CONSTRAINT FK_user_has_lecture_lectures FOREIGN KEY (lecture_id) REFERENCES lectures (id)
END
;

BEGIN
ALTER TABLE user_has_lecture ADD CONSTRAINT FK_user_has_lecture_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- event_defs
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'event_defs')
BEGIN
    DECLARE @reftable_21 nvarchar(60), @constraintname_21 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'event_defs'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_21, @constraintname_21
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_21+' drop constraint '+@constraintname_21)
        FETCH NEXT from refcursor into @reftable_21, @constraintname_21
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE event_defs
END

CREATE TABLE event_defs
(
    id INT NOT NULL IDENTITY,
    name_pl VARCHAR(250) NOT NULL,
    name_en VARCHAR(250) NULL,
    code VARCHAR(100) NOT NULL,
    css_class VARCHAR(50) NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT event_defs_PK PRIMARY KEY (id)
);

CREATE INDEX PK_event_defs ON event_defs (id);

-----------------------------------------------------------------------
-- roles
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'roles')
BEGIN
    DECLARE @reftable_22 nvarchar(60), @constraintname_22 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'roles'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_22, @constraintname_22
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_22+' drop constraint '+@constraintname_22)
        FETCH NEXT from refcursor into @reftable_22, @constraintname_22
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE roles
END

CREATE TABLE roles
(
    id INT NOT NULL IDENTITY,
    code VARCHAR(30) NOT NULL,
    name_pl VARCHAR(60) NOT NULL,
    name_en VARCHAR(60) NOT NULL,
    last_modified DATETIME(16,3) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT roles_PK PRIMARY KEY (id)
);

CREATE INDEX PK_roles ON roles (id);

-----------------------------------------------------------------------
-- user_roles
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_roles_roles')
    ALTER TABLE user_roles DROP CONSTRAINT FK_user_roles_roles;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_roles_users')
    ALTER TABLE user_roles DROP CONSTRAINT FK_user_roles_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'user_roles')
BEGIN
    DECLARE @reftable_23 nvarchar(60), @constraintname_23 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'user_roles'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_23, @constraintname_23
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_23+' drop constraint '+@constraintname_23)
        FETCH NEXT from refcursor into @reftable_23, @constraintname_23
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE user_roles
END

CREATE TABLE user_roles
(
    user_id INT NOT NULL,
    role_id INT NOT NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT user_roles_PK PRIMARY KEY (user_id,role_id)
);

CREATE INDEX PK_user_roles ON user_roles (user_id,role_id);

BEGIN
ALTER TABLE user_roles ADD CONSTRAINT FK_user_roles_roles FOREIGN KEY (role_id) REFERENCES roles (id)
END
;

BEGIN
ALTER TABLE user_roles ADD CONSTRAINT FK_user_roles_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- system_logs
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_system_logs_users')
    ALTER TABLE system_logs DROP CONSTRAINT FK_system_logs_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'system_logs')
BEGIN
    DECLARE @reftable_24 nvarchar(60), @constraintname_24 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'system_logs'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_24, @constraintname_24
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_24+' drop constraint '+@constraintname_24)
        FETCH NEXT from refcursor into @reftable_24, @constraintname_24
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE system_logs
END

CREATE TABLE system_logs
(
    id INT NOT NULL IDENTITY,
    user_id INT NULL,
    ext_ip VARCHAR(15) NOT NULL,
    message VARCHAR(MAX)(2147483647) NULL,
    stack_trace VARCHAR(MAX)(2147483647) NULL,
    created DATETIME(16,3) DEFAULT '' NOT NULL,
    CONSTRAINT system_logs_PK PRIMARY KEY (id)
);

CREATE INDEX PK_system_logs ON system_logs (id);

BEGIN
ALTER TABLE system_logs ADD CONSTRAINT FK_system_logs_users FOREIGN KEY (user_id) REFERENCES users (id)
END
;

-----------------------------------------------------------------------
-- user_calerndars
-----------------------------------------------------------------------

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_calerndars_calendars')
    ALTER TABLE user_calerndars DROP CONSTRAINT FK_user_calerndars_calendars;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='FK_user_calerndars_users')
    ALTER TABLE user_calerndars DROP CONSTRAINT FK_user_calerndars_users;

IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'user_calerndars')
BEGIN
    DECLARE @reftable_25 nvarchar(60), @constraintname_25 nvarchar(60)
    DECLARE refcursor CURSOR FOR
    select reftables.name tablename, cons.name constraintname
        from sysobjects tables,
            sysobjects reftables,
            sysobjects cons,
            sysreferences ref
        where tables.id = ref.rkeyid
            and cons.id = ref.constid
            and reftables.id = ref.fkeyid
            and tables.name = 'user_calerndars'
    OPEN refcursor
    FETCH NEXT from refcursor into @reftable_25, @constraintname_25
    while @@FETCH_STATUS = 0
    BEGIN
        exec ('alter table '+@reftable_25+' drop constraint '+@constraintname_25)
        FETCH NEXT from refcursor into @reftable_25, @constraintname_25
    END
    CLOSE refcursor
    DEALLOCATE refcursor
    DROP TABLE user_calerndars
END

CREATE TABLE user_calerndars
(
    id INT NOT NULL,
    user_id INT NOT NULL,
    calendar_id INT NOT NULL,
    active_start DATETIME(16,3) NOT NULL,
    active_end DATETIME(16,3) NULL,
    CONSTRAINT user_calerndars_PK PRIMARY KEY (id)
);

CREATE INDEX PK_user_calerndars ON user_calerndars (id);

BEGIN
ALTER TABLE user_calerndars ADD CONSTRAINT FK_user_calerndars_calendars FOREIGN KEY (calendar_id) REFERENCES calendars (id)
END
;

BEGIN
ALTER TABLE user_calerndars ADD CONSTRAINT FK_user_calerndars_users FOREIGN KEY (user_id) REFERENCES calendars (id)
END
;
